package com.hlju.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hlju.reggie.bean.Category;
import com.hlju.reggie.bean.Dish;
import com.hlju.reggie.bean.Flavor;
import com.hlju.reggie.bean.dto.DishDto;
import com.hlju.reggie.common.R;
import com.hlju.reggie.service.CategoryService;
import com.hlju.reggie.service.DishService;
import com.hlju.reggie.service.FlavorService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@RestController
@RequestMapping("/dish")
public class DishController {

    @Autowired
    private DishService dishService;
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private FlavorService flavorService;
    @Autowired
    private RedisTemplate redisTemplate;

    //分页查询
    @GetMapping("/page")
    public R<Page> page(Integer page, Integer pageSize, String name) {
        //创建菜品分页对象
        Page<Dish> pageInfo = new Page<>(page, pageSize);
        //创建条件构造器
        QueryWrapper<Dish> wrapper = new QueryWrapper<>();
        //设置排序
        wrapper.orderByDesc("sort");
        //如果传递过来的name不为空 作为条件模糊查询
        if (name != null && !name.equals("")) {
            wrapper.like("name", name);
        }
        //执行分页处理
        dishService.page(pageInfo, wrapper);

        //创建菜品dto的分页对
        Page<DishDto> dtoPage = new Page<>();

        //对象的属性复制(将菜品的分页对象中菜品集合属性复制、排除records)
        BeanUtils.copyProperties(pageInfo, dtoPage, "records");

        //获取菜品分页对象的菜品集合数据
        List<Dish> dishList = pageInfo.getRecords();

        //创建一个dto的菜品对象集合
        List<DishDto> dtoList = new ArrayList<>();
        //遍历菜品集合
        for (Dish dish : dishList) {
            //创建dto对象
            DishDto dto = new DishDto();
            //将当前dish对象的属性复制到dto中
            BeanUtils.copyProperties(dish, dto);
            //根据分类id查询分类对象
            Category category = categoryService.getById(dish.getCategoryId());
            //如果当前分类不为空
            if (category != null) {
                //给dto对象设置分类名称
                dto.setCategoryName(category.getName());
            }
            //将dto对象添加进dto集合
            dtoList.add(dto);
        }
        //给dto的分页对象 重新设置集合
        dtoPage.setRecords(dtoList);
        //返回dto分页对象
        return R.sucess(dtoPage);
    }

    //保存菜品
    @CacheEvict(value = "dishCache",allEntries = true)
    @PostMapping
    public R<String> save(@RequestBody DishDto dto) {
        //调用业务层方法
        dishService.saveDish(dto);
        return R.sucess("添加菜品成功");
    }

    //根据id获取菜品
    @GetMapping("/{id}")
    public R<DishDto> getDishWithFlavors(@PathVariable("id") Long id) {
        return R.sucess(dishService.getDishWithFlavors(id));
    }

    //修改菜品
    @CacheEvict(value = "dishCache",allEntries = true)
    @PutMapping
    public R<String> updateDishWithFlavors(@RequestBody DishDto dto) {
        dishService.updateDishWithFlavors(dto);
        return R.sucess("修改菜品成功");
    }

    //修改菜品状态
    @CacheEvict(value = "dishCache",allEntries = true)
    @PostMapping("/status/{status}")
    public R<String> updateStatus(@PathVariable("status") Integer status, Long[] ids) {

       /* for (Long id : ids) {
            Dish dish = new Dish();
            dish.setId(id);
            dish.setStatus(status);
            dishService.updateById(dish);
        }*/
        List<Dish> dishList = new ArrayList<>();
        for (Long id : ids) {
            Dish dish = new Dish();
            dish.setId(id);
            dish.setStatus(status);
            dishList.add(dish);
        }
        dishService.updateBatchById(dishList);
        return R.sucess("修改成功");
    }

    //删除菜品
    @CacheEvict(value = "dishCache",allEntries = true)
    @DeleteMapping
    public R<String> deleteDish(Long[] ids) {
        return dishService.removeByIds(Arrays.asList(ids)) ? R.sucess("删除成功") : R.error("删除失败");
    }

    //根据分类ID查询菜品列表
    @Cacheable(value = "dishCache",key = "#dish.categoryId+'_'+#dish.status")
    @GetMapping("/list")
    public R<List> list(Dish dish) {
        //创建条件构造器
        QueryWrapper<Dish> wrapper = new QueryWrapper<>();
        wrapper.eq("category_id", dish.getCategoryId());
        wrapper.eq("status", dish.getStatus());
        wrapper.orderByDesc("sort");
        //执行查询
        List<Dish> dishList = dishService.list(wrapper);

        //创建dto的集合
        List<DishDto >dtoList = new ArrayList<>();
        //遍历菜品集合
        for (Dish d : dishList) {
            //创建dto对象
            DishDto dto = new DishDto();
            //属性复制
            BeanUtils.copyProperties(d, dto);
            //通过菜品id查询口味列表
            QueryWrapper<Flavor> flavorQueryWrapper = new QueryWrapper<>();
            flavorQueryWrapper.eq("dish_id", d.getId());
            List<Flavor> flavorList = flavorService.list(flavorQueryWrapper);
            //将口味集合设置给dto
            dto.setFlavors(flavorList);
            //将dto添加进dto的集合
            dtoList.add(dto);
        }
        return R.sucess(dtoList);
    }
}
